<?php

class LetsEventsListener{
  /**
   * Create virtual member for association and money account
   * @param sfEvent $event
   */
  public static function associationCreatedEvent(sfEvent $event)
  {
    $params = $event->getParameters();
    $asso = $params['association'];
    $assoId = $asso->getId();
    // create special status
    $status = new Status();
    $status->setLabel('Compte association');
    $status->setAssociationId($assoId);
    $status->setState(StatusTable::STATE_DISABLED);
    $status->save();
    // create member for association
    $member = new Member();
    $member->setFirstname('Association');
    $member->setLastname($asso->getName());
    $member->setDueExempt(true);
    $assoUsername = 'association'.$assoId;
    $member->setUsername($assoUsername);
    $member->setAssociationId($assoId);
    $member->setSubscriptionDate(date('Y-m-d'));
    $member->setStatusId($status->getId());
    $member->setState(MemberTable::STATE_ENABLED);
    //		$member->setPassword('');
    $member->save();

    // create money account for association
    $assoAccount = new LetsMoneyAccount();
    $assoAccount->setAccountType(LetsMoneyAccountTable::TYPE_ASSOCIATION);
    $assoAccount->setMemberId($member->getId());
    $assoAccount->setInitAmount(0);
    $assoAccount->save();
    // load data for association

  }
  /**
   * Create money account for member, add default money from association account
   * @param sfEvent $event
   */
  public static function memberCreatedEvent(sfEvent $event)
  {
    $params = $event->getParameters();
    $member = $params['member'];
    $memberId = $member->getId();
    $memberAccount = new LetsMoneyAccount();
    $memberAccount->setAccountType(LetsMoneyAccountTable::TYPE_MEMBER);
    $memberAccount->setMemberId($memberId);
    $memberAccount->setInitAmount(0);
    $memberAccount->save();
    $assoId = $member->getAssociationId();

    if($params['first'] == false)
    {
      $money = LetsConfiguration::moneyNewMember($assoId);
      if($money != 0)
      {
        $echange = new LetsExchange();

        $assoAcountId = LetsMoneyAccountTable::getAccountIdByAssociationId($assoId);

        $memberAccountId = $memberAccount->getId();
        // association is debitor
        if($money > 0)
        {
          $echange->setCreditorId($memberAccountId);
          $echange->setDebitorId($assoAcountId);
        }
        else{
          $echange->setCreditorId($assoAccountId);
          $echange->setDebitorId($memberAccountId);
        }
        $echange->setAmount(abs($money));
        $echange->setName('Pret pour commencer en tant que nouveau adhérent.');

        $assoCategory = LetsMainCategoryTable::getAssociationServiceCategory($assoId);
        $echange->setCategoryId($assoCategory->getId());
        $echange->setCreatedBy($event->getSubject()->getUser()->getUserId());
        $echange->setStateValidation(LetsExchangeTable::STATE_ACCEPTED);
        $now = date('Y-m-d');
        $echange->setDateValidation($now);
        $echange->setDateExchange($now);
        $echange->save();
      }
    }
  }

}
